import OBJ from 'webgl-obj-loader'
type Dot = `${string}.fs` | `${string}.vs`
type ArrShaderFileLoaded = [`vs`, `fs`];
type OBJFileExt = `${string}.obj`

export const readShaderFile = async <T extends string>(filePath: T extends Dot ? never : T): Promise<ArrShaderFileLoaded> => {
    const vs = new XMLHttpRequest();
    const fs = new XMLHttpRequest();
    return <ArrShaderFileLoaded>(await Promise.all(
        [vs, fs].map((req, i) => {
            return new Promise<string>((suc, err) => {
                req.onreadystatechange = function () {
                    if (req.readyState === 4 && req.status === 200) {
                        suc(req.responseText)
                    }
                }
                req.open('GET', filePath + (i == 0 ? '.vs' : '.fs'), true);
                req.send();
            })
        })))
}

export const readOBJtoMesh = async <T extends string>(objStr: T extends OBJFileExt ? T : never) => {
    return new OBJ.Mesh(objStr);
}